import 'package:flutter/material.dart';
class MyNumber extends StatefulWidget {
  MyNumber({Key? key, required this.num, required this.min, required this.max, required this.step, required this.numberChange}) : super(key: key);
  late int num;
  final int min;
  final int max;
  final int step;
  final numberChange;

  @override
  _MyNumberState createState() => _MyNumberState();
}

class _MyNumberState extends State<MyNumber> {
  late bool exMax;
  late bool exMin;
  @override
  void initState() {
    exMax = widget.num >= widget.max?true:false;
    exMin = widget.num <= widget.min?true:false;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        MaterialButton(
          onPressed: exMin?null:() {
            if(widget.num > widget.min) {
              widget.num = widget.num - widget.step;
              if(widget.num <= widget.min) {
                widget.num = widget.min;
              }
              setState(() {
                exMax = widget.num >= widget.max?true:false;
                exMin = widget.num <= widget.min?true:false;
              });
              widget.numberChange(widget.num);
            }
          },
          child: Icon(Icons.remove, color: exMin?const Color(0xffd3d5d6):const Color(0xffb3b5b6),),
          color: exMin?const Color(0xffefefef):const Color(0xffdfdfdf),
          minWidth: 30,
          height: 30,
          padding: EdgeInsets.zero,
          //按钮的阴影
          elevation: 0,
          disabledColor: const Color(0xffefefef),
        ),
        Container(
          padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
          height: 30,
          decoration: const BoxDecoration(
            color: Color(0xffdedede),
            borderRadius: BorderRadius.all(Radius.circular(2.0)),
          ),
          child: Center(
            child: Text('${widget.num}'),
          ),
        ),
        MaterialButton(
          onPressed: exMax?null:(){
            if(widget.num < widget.max) {
              widget.num = widget.num + widget.step;
              if(widget.num >= widget.max) {
                widget.num = widget.max;
              }
              setState(() {
                exMax = widget.num >= widget.max?true:false;
                exMin = widget.num <= widget.min?true:false;
              });
              widget.numberChange(widget.num);
            }
          },
          child: Icon(Icons.add, color: exMax?const Color(0xffd3d5d6):const Color(0xffb3b5b6),),
          color: exMax?const Color(0xffefefef):const Color(0xffdfdfdf),
          minWidth: 30,
          height: 30,
          padding: EdgeInsets.zero,
          //按钮的阴影
          elevation: 0,
          disabledColor: const Color(0xffefefef),
        ),
      ],
    );
  }
}
